<template>
  <div class="edit-page-bg">
    <el-card class="edit-card">
      <el-page-header @back="goBack" content="文章编辑" class="edit-header" />
      <el-form :model="form" label-width="80px" class="edit-form">
        <el-form-item label="标题">
          <el-input v-model="form.title" placeholder="请输入标题" class="edit-title-input" />
        </el-form-item>
        <el-form-item label="内容">
          <div class="editor-wrapper">
            <RichEditor v-model="form.content" />
          </div>
        </el-form-item>
        <el-form-item label="标签">
          <el-select
            v-model="form.tags"
            multiple
            filterable
            placeholder="请选择文章标签"
            style="width: 100%"
          >
            <el-option
              v-for="tag in tags"
              :key="tag.id"
              :label="tag.name"
              :value="tag.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" class="edit-status-select">
            <el-option label="草稿" value="draft" />
            <el-option label="已发布" value="published" />
          </el-select>
        </el-form-item>
        <el-form-item class="edit-btns">
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<!-- 文章编辑页面 -->
<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { fetchArticle, createArticle, updateArticle } from '@/api/article'
import { fetchAllTags } from '@/api/tag'
import RichEditor from '@/components/RichEditor.vue'

const route = useRoute()
const router = useRouter()
const articleId = route.query.id // ?id=xxx
const tags = ref([]) // 所有标签列表

const form = ref({
  title: '',
  content: '',
  status: 'draft',
  tags: [] // 文章选中的标签ID数组
})

const loadTags = async () => {
  try {
    const res = await fetchAllTags()
    tags.value = res.data
  } catch (error) {
    console.error('加载标签失败', error)
  }
}

const loadArticle = async () => {
  if (articleId) {
    const res = await fetchArticle(articleId)
    form.value = {
      title: res.data.title,
      content: res.data.content,
      status: res.data.status,
      tags: res.data.tags?.map(tag => tag.id) || [] // 获取已选标签ID
    }
  }
}

const submitForm = async () => {
  if (articleId) {
    await updateArticle(articleId, form.value)
  } else {
    await createArticle(form.value)
  }
  // 依据现在的状态，跳转到不同的页面
  if (form.value.status === 'draft') {
    router.push('/draft')
  } else {
    router.push('/articles')
  }
}

const goBack = () => {
  if (form.value.status === 'draft') {
    router.push('/draft')
  } else {
    router.push('/articles')
  }
}

onMounted(async () => {
  await loadTags() // 先加载所有标签
  await loadArticle() // 再加载文章数据
})
</script>

<style scoped>
.edit-page-bg {
  min-height: 100vh;
  background: #f6f8fa;
  padding: 40px 0;
}
.edit-card {
  max-width: 900px;
  margin: 0 auto;
  box-shadow: 0 4px 24px 0 rgba(0,0,0,0.07);
  border-radius: 12px;
  padding: 32px 36px 24px 36px;
}
.edit-header {
  margin-bottom: 32px;
}
.edit-form {
  margin-top: 10px;
}
.edit-title-input {
  font-weight: bold;
  font-size: 18px;
}
.editor-wrapper {
  width: 100%;
  min-width: 300px;
  max-width: 100%;
}
.edit-status-select {
  width: 180px;
}
.edit-btns {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  margin-top: 24px;
}
</style> 